package com.rabbit.vo;

import com.rabbit.constant.ResultCodeEnum;
import com.rabbit.constant.SecurityConstant;
import com.rabbit.handler.RequestContext;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * <h1>通用返回</h1>
 *
 * @author zjw
 * @date 2020/09/06
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ResultVO<T> {

    /***
     * 状态码
     */
    @ApiModelProperty(value = "状态码")
    private Integer code;

    /***
     * 消息
     */
    @ApiModelProperty(value = "消息")
    private String msg;

    /**
     * traceId
     */
    @ApiModelProperty(value = "traceId")
    private String traceId;

    /***
     * 返回对象
     */
    @ApiModelProperty(value = "返回对象")
    private T data;

    public ResultVO(ResultCodeEnum resultCodeEnum) {
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMessage();
        this.traceId = getTraceId();
    }

    public ResultVO(ResultCodeEnum resultCodeEnum, T data) {
        this.code = resultCodeEnum.getCode();
        this.msg = resultCodeEnum.getMessage();
        this.data = data;
        this.traceId = getTraceId();
    }

    public ResultVO(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
        this.traceId = getTraceId();
    }

    private String getTraceId() {
        return RequestContext.get(SecurityConstant.LOG_TRACE_ID).toString();
    }

}
